package com.pignest.hotel.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;

import com.pignest.hotel.system.Logic;


public class DateUtil {
	
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String HOTEL_DEFAULT_FORMAT = "MM-dd HH:mm";
	
	private static final String DEFAULT_FORMAT = DEFAULT_DATETIME_FORMAT;
	
	public static final String TIME_FORMAT = "HH:mm";

/*	private static final double[] LIMITS = { 0, 1, 2 };

	private static final String[] MINUTES_PART = { "", "1 minute ", "{0,number} minutes " };
	private static final String[] SECONDS_PART = { "0 seconds", "1 second", "{1,number} seconds" };
	private static final ChoiceFormat MINUTES_FORMAT = new ChoiceFormat(LIMITS, MINUTES_PART);
	private static final ChoiceFormat SECONDS_FORMAT = new ChoiceFormat(LIMITS, SECONDS_PART);
	private static final MessageFormat MINUTE_SECONDS = new MessageFormat( "{0}{1}");

	static {
		MINUTE_SECONDS.setFormat(0, MINUTES_FORMAT);
		MINUTE_SECONDS.setFormat(1, SECONDS_FORMAT);
	}
*/
	public static final long ONE_HOUR = 60 * 60 * 1000;
	public static final long HALF_DAY = 12 * ONE_HOUR;
	public static final long ONE_DAY = 24 * ONE_HOUR;
	public static final long TWO_DAY = 2 * ONE_DAY;
	public static final long ONE_WEEK = 7 * ONE_DAY;

	public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);

	public static final SimpleDateFormat DATE_FORMATL = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
	
	public static final SimpleDateFormat DEFAULT_FORMATL = new SimpleDateFormat(DEFAULT_FORMAT);

	public static final SimpleDateFormat HOTEL_FORMATL = new SimpleDateFormat(HOTEL_DEFAULT_FORMAT);
	
	public static String toString(Date date, String format) {
		if ((date == null) || (format == null) || (format.length() == 0)) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	public static String toString(Date date) {
		if (date == null)
			return "";
		return DEFAULT_FORMATL.format(date);
	}
	public static String toStringHotel(Date date) {
		if (date == null)
			return "";
		return HOTEL_FORMATL.format(date);
	}

	public static String toFullString(Date date) {
		if (date == null)
			return "";
		return DATE_TIME_FORMAT.format(date);
	}

	public static String toString(long time) {
		return toString(new Date(time));
	}

	public static Date toDate(String str, String format) {
		if ((str == null) || (str.length() == 0) || (format == null)
				|| (format.length() == 0)) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatter.setLenient(false);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(str, pos);
	}

	public static boolean compare(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return true;
		}
		if (date1 == null || date2 == null)
			return false;
		else
			return date1.getTime() == date2.getTime();
	}

	public static Date toDate(String str) {
		try {
			if (str.indexOf(':') > 0) {
				return DATE_TIME_FORMAT.parse(str);
			} else {
				return DATE_FORMATL.parse(str);
			}
		} catch (Exception ex) {
			return null;
		}
	}

	public static String currentDateToString(String format) {
		Date date = new Date();
		return toString(date, format);
	}

	public static String curDateStr() {
		return DATE_FORMATL.format(new Date());
	}

	public static String curDateTimeStr() {
		return DATE_TIME_FORMAT.format(new Date());
	}

/*	public static String formatElapsedTime(long millis) {
		long seconds = millis / 1000;
		long minutes = seconds / 60;
		Object[] args = { new Long(minutes), new Long(seconds % 60) };
		return MINUTE_SECONDS.format(args);
	}
*/
	public static Date getStartOfDay(Date d) {
		if (d == null) {
			return null;
		}

		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(d);
		setTodayOrigin(cal);

		return cal.getTime();
	}

	private static void setTodayOrigin(GregorianCalendar cal1) {
		cal1.set(Calendar.AM_PM, Calendar.AM);
		cal1.set(Calendar.HOUR, 8); // 时区
		cal1.set(Calendar.MINUTE, 0);
		cal1.set(Calendar.SECOND, 0);
		cal1.set(Calendar.MILLISECOND, 0);
	}

	public static int getDayOfMonth(Date date) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return day;
	}

	public static Date getFirstDayOfMonth(Date curDate) {
		GregorianCalendar currentDay = new GregorianCalendar();
		currentDay.setTime(curDate);
		// int today = currentDay.get(Calendar.DAY_OF_MONTH);
		int month = currentDay.get(Calendar.MONTH);
		int year = currentDay.get(Calendar.YEAR);

		Calendar thisMonth = Calendar.getInstance();
		thisMonth.set(Calendar.MONTH, month);
		thisMonth.set(Calendar.YEAR, year);
		thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
		thisMonth.set(Calendar.DAY_OF_MONTH, 1);
		thisMonth.set(Calendar.AM_PM, Calendar.AM);
		thisMonth.set(Calendar.HOUR, 0);
		thisMonth.set(Calendar.MINUTE, 0);
		thisMonth.set(Calendar.SECOND, 0);
		thisMonth.set(Calendar.MILLISECOND, 0);

		return thisMonth.getTime();
	}
	
	/** 解析小时分钟秒 */
	public static int[] parseHMS(String time) {
		try {
			String[] hmm = time.split(":");
			int[] r = new int[3];
			if (hmm == null || hmm.length == 0)
				return r;
			r[0] = Integer.parseInt(hmm[0]);
			if (hmm.length > 1)
				r[1] = Integer.parseInt(hmm[1]);
			if (hmm.length > 2)
				r[2] = Integer.parseInt(hmm[2]);
			return r;
		} catch (Exception e) {
		}
		return null;
	}
	
	public static Date parseDate(Date date, String hms) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int[] time = parseHMS(hms);
		if (time != null) {
			cal.set(Calendar.HOUR_OF_DAY, time[0]);
			cal.set(Calendar.MINUTE, time[1]);
			cal.set(Calendar.SECOND, time[2]);
			cal.set(Calendar.MILLISECOND, 0);
		}
		return cal.getTime();
	}
	/**
	 * 计算住店天数
	 * (1)当天结账的
	 *   1.1 入住在下午2点之前的
	 *     1.1.1结账在2点之前的0.5天
	 *     1.1.2结账在8点之前的0.5
	 *   1.2入住在下午2点之后的
	 * (2)隔天结账的
         2.1不超过第二天的二点不加收
         2.2 超过第二天的8点加收半天
         2.3 超过第二天8点加收1天
	 * **/
	public static double parseLiveDays(String livingTime,String checkOutTime){
		double re=0;
//		if(livingTime==null||livingTime.equals(""))
//			return 0;
//		if(checkOutTime==null||livingTime.equals(""))
//			return 0;
//		//验证时间先后是否有误
//		if(!isDateBefore(livingTime,checkOutTime)){
//			return 0;
//		}
//		double bedays=betweenDays(livingTime,checkOutTime);
//		Date livingDate=toDate(livingTime,DEFAULT_DATETIME_FORMAT);
//		Date curCheckOutDate=toDate(checkOutTime,DEFAULT_DATETIME_FORMAT);
//		int checkOutHours=curCheckOutDate.getHours();
//		int livingHours =livingDate.getHours();
//		//如果是当天结账
//	    if(bedays==0){
//	    	if(livingHours<Config.getNoonPoint())//入住在下午2点之前的
//	    	{
//	    		if(checkOutHours<Config.getNoonPoint())//结账在2点之前的0.5天
//	    			re=0.5;
//	    		else if(checkOutHours>Config.getNoonPoint()&&checkOutHours<Config.getCheckAuditTime())
//	    			re=1;
//	    		else
//	    			re=0.5;
//	    	}else//入住在下午2点之后的
//	    	{
//	    		if(checkOutHours<=Config.getEveningTimePoint())//结账在8点之前的0.5
//	    			re=0.5;
//	    		else if(checkOutHours>Config.getEveningTimePoint()&&checkOutHours<Config.getCheckAuditTime())
//	    			re=1;
//	    		else
//	    			re=0.5;
//	    	}
//	    }else{
//	    	if(checkOutHours<=Config.getNoonPoint()){//不超过第二天的二点不加收
//	    		re=0;
//	    	}else if(checkOutHours>=Config.getNoonPoint()&&checkOutHours<Config.getEveningTimePoint()){//超过第二天的8点加收半天
//	    		re=0.5;
//	    	}else if(checkOutHours>=Config.getEveningTimePoint()&&checkOutHours<Config.getCheckAuditTime())
//	    		re=1;
//	    }
		return re;
	}
	/**
	 * 判断时间date1是否在时间date2之前
	 * 时间格式 2010-4-21 16:16:34
	 * */
	 public static boolean isDateBefore(String date1,String date2){
	  try{
	   DateFormat df = DateFormat.getDateTimeInstance();
	   return df.parse(date1).before(df.parse(date2));
	  }catch(ParseException e){
	   System.out.print("[SYS] " + e.getMessage());
	   return false;
	  }
	 }
	 /**  
      * 两个时间之间的天数  
      *   
      * @param date1  
      * @param date2  
      * @return  
      */
	public static long betweenDays(String date2, String date1) {
		if (StringUtils.isEmpty(date1) || StringUtils.isEmpty(date2))
			return 0;
		// 转换为标准时间
		java.util.Date date = null;
		java.util.Date mydate = null;
		try {
			date = DATE_FORMATL.parse(date1);
			mydate = DATE_FORMATL.parse(date2);
		} catch (Exception e) {
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);

		return day;
	}  
   public static int betweenHours(Date date1,Date date2){
	   if(date1==null||date2==null)
		   return 0;
	   int i=(int)(date2.getTime()-date1.getTime())/(60*1000);
	   return i;
   }
   
   public static int afterHour(int hour){
	   Calendar cal = Calendar.getInstance();
	  // cal.setTime(new Date());
       cal.add(cal.HOUR_OF_DAY, hour);
	   return cal.get(Calendar.HOUR_OF_DAY);

   }
	 public static void main(String[] args){
	/*	  boolean flag = isDateBefore("2004-09-09 12:12:12","2005-09-09 16:00:00");
		  System.out.println(flag);
		  flag = isDateBefore("2006-09-09 01:01:01","2005-09-09 16:00:00");
		  System.out.println(flag);*/
		 // long days=betweenDays("2010-05-09 23:59:59","2010-05-08 01:01:01");
		  //System.out.println(days);
		 //double t=parseLiveDays("2010-05-08 20:01:01","2010-05-09 20:01:01");
		 //System.out.println(t);
		// String s =currentDateToString("yyMMddHHmmss");
		// System.out.println(s);
		 //System.out.println(afterHour(3));
		 Date l =toDate("2010-09-18 11:01:00","yyyy-MM-dd HH:mm:ss");
		 Date l1=toDate("2010-09-18 20:20:00","yyyy-MM-dd HH:mm:ss");
		 /*int s =betweenHours(l,l1);
		 System.out.println(s);*/
		 System.out.println(Logic.countUnionHours(l1, l));
		
		 }


}
